<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  
<html xmlns = "http://www.w3.org/1999/xhtml">
  <!-- 2012 lukbrasil -->
  <!-- Internet & World Wide Web How To Program - Exercise 9.26 -->
  <head>
    <title>Exercise 9.26</title>
    
    <script type = "text/javascript">
      <!--      
      var tailsTotal = 0;
      var headsTotal = 0;
      
      // false is Tails
      // true is Heads
      function flip() {
        return Math.floor(1 + Math.random() * 2) == 1;
      }
      
      function update(element, value) {
        document.getElementById(element).innerHTML = value;
      }
      
      function toss() {
        flip() ? ++headsTotal : ++tailsTotal;
        
        update("tailsTotal", tailsTotal);
        update("tailsTotalPercent", parseInt(tailsTotal / (tailsTotal + headsTotal) * 100));
        
        update("headsTotal", headsTotal);
        update("headsTotalPercent", parseInt(headsTotal / (tailsTotal + headsTotal) * 100));
        
        update("total", headsTotal + headsTotal);
      }
      // -->
    </script>
  </head>
  <body>
    <form action = "">
      <table>
        <thead>
          <tr>
            <th>Side</th>
            <th>Total</th>
            <th>Total %</th>
          </tr>
        </thead>
        
        <tbody>          
          <tr>
            <td>Tails</td>
            <td id = "tailsTotal">0</td>
            <td id = "tailsTotalPercent">0</td>
          </tr>
          
          <tr>
            <td>Heads</td>
            <td id = "headsTotal">0</td>
            <td id = "headsTotalPercent">0</td>
          </tr>
          
          <tr>
            <td></td>
            <td id = "total">0</td>
            <td/>
          </tr>
        </tbody>
      </table>
      
      <div><input type = "button" value = "Toss" onclick = "toss()"</div>      
    </form>
  </body>
</html>
